<script>
$(document).ready(function () {
    var offset = 50,
        duration = 500,
        width = 960;
    $(window).scroll(function () {
        if ($(window).width() > width) {
            if ($(this).scrollTop() > offset) {
                $('footer').css('top', '20px');
                $('footer .back-to-top').fadeIn(duration);
            } else {
                $('footer').css('top', 'auto');
                $('footer .back-to-top').fadeOut(duration);
            }
        }
    });
    $(window).resize(function () {
        if ($(window).width() < width) {
            $('footer').css('top', 'auto');
            $('footer .back-to-top').fadeOut(duration);
        }
        if ($(window).width() >= width && $(this).scrollTop() > offset) {
            $('footer').css('top', '20px');
            $('footer .back-to-top').fadeIn(duration);
        }
    });

    $('footer .back-to-top, .gotop').on('click', function (event) {
        sendGaEvent('Blog', 'Back to Top', event.currentTarget);
        event.preventDefault();
        $('html, body').animate({
            scrollTop: 0
        }, duration);
        return false;
    });

    $('.show-hidden').on('click', function () {
        sendGaEvent('Post', 'Show Hidden', event.currentTarget);
        $(this).parent().next().toggleClass("hidden");
        $(this).toggleClass("hidden");
    });
});
</script>

<!-- Google Analytics Event tracking -->
{% if site.google_analytics %}
<script>
$(document).on('click', 'a.internal', function(event) {
    sendGaEvent('Blog', 'Navigate', event.currentTarget);
});
$('.the-header').on('click', '.nav-link a, [class$=wrapper] .results a, .nav-link button', function(event) {
    sendGaEvent('Blog', 'Navigate', event.currentTarget);
});
$('.the-header').on('click', '.nav-link button', function(event) {
    sendGaEvent('Blog', 'Navigate', event.currentTarget);
});
$('.the-footer').on('click', '.about h4 a', function(event) {
    sendGaEvent('Blog', 'Go to About', event.currentTarget);
});
$('.the-footer').on('click', '.social-links a', function(event) {
    sendGaEvent('Blog', 'Click Social Link', event.currentTarget);
});
$('.unit-article').on('click', '.tag_box.categories a', function(event) {
    sendGaEvent('Blog', 'Click Category', event.currentTarget);
});
$('.unit-article').on('click', '.tag_box.tags a', function(event) {
    sendGaEvent('Blog', 'Click Tag', event.currentTarget);
});
$('.unit-article').on('click', '.comments .show-hidden', function(event) {
    sendGaEvent('Post', 'Show Comments', event.currentTarget);
});
$('.unit-article').on('click', '.meta .author', function(event) {
    sendGaEvent('Post', 'Show Author', event.currentTarget);
});
$('.unit-article').on('click', '.meta .license-icon', function(event) {
    sendGaEvent('Post', 'Show License', event.currentTarget);
});
$('.unit-article').on('click', '.meta .list-tag a', function(event) {
    sendGaEvent('Post', 'Click Tag', event.currentTarget);
});
$('.unit-article').on('click', '.meta .list-category', function(event) {
    sendGaEvent('Post', 'Click Category', event.currentTarget);
});
$('.unit-foot').delegate('.addthis_toolbox .btn-share-post', 'click', function(event) {
    sendGaEvent('Post', 'Share Post', event.currentTarget);
});
$('.search-wrapper').on('click', '.results a', function(event) {
    sendGaEvent('Search', 'Click Search Result', event.currentTarget);
});
$('.search-wrapper').on('click', '.btn-close', function(event) {
    sendGaEvent('Search', 'Close Search', event.currentTarget);
});
</script>
{% endif %}

<!-- Show menu overlay + Jekyll Simple Search option -->
<script src="{{ site.baseurl }}/assets/javascripts/jekyll-search.jquery.js"></script>
<script>
$(document).ready(function () {
    $('.search-field').simpleJekyllSearch({
      jsonFile: '{{ site.baseurl }}/search.json',
      template: '<li><a href="{{ site.baseurl }}{url}">{title} <span class="entry-date"><time datetime="{date}">{shortdate}</time></span></a></li>',
      searchResults: '.search-wrapper .results',
      searchResultsTitle: '<h4>{{ site.data.i18n[site.lang].search_results }}</h4>',
      noResults: '<p>{{ site.data.i18n[site.lang].search_not_found }}</p>'
    });
});

(function ($, window, undefined) {
    var closeOverlay = function () {
        $('.nav-wrapper, .search-wrapper').removeAttr('style');
        $(".nav-form, .search-form").removeClass('active');
        $("body").removeClass('nav-overlay search-overlay');
    };

    $('.nav-global .btn-search').on('click', function () {
        $('.search-wrapper').css({display: "block"});
        $(".search-form").addClass('active');
        $(".search-form").find('input').focus();
        $("body").addClass('search-overlay');
    });

    $('.nav-global .btn-menu').on('click', function () {
        $('.nav-wrapper').css({display: "block"});
        $(".nav-form").addClass('active');
        $(".nav-form .search-field").prop('disabled', true);
        $("body").addClass('nav-overlay');
    });

    $('.nav-wrapper .btn-close, .search-wrapper .btn-close').on('click', function () {
        closeOverlay();
    });

    $(document).on('keyup', function (e) {
        if (e.keyCode === 27) {
            closeOverlay();
        }
    });
})(jQuery, window);
</script>

<script src='//cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.4/jquery.fancybox.min.js'></script>
<script src='//cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.4/helpers/jquery.fancybox-buttons.min.js'></script>
<script src="{{ site.baseurl }}/assets/javascripts/unveil/jquery.unveil.min.js"></script>

<script>
    window.jQuery.fancybox || document.write('<script src="{{ site.baseurl }}/assets/javascripts/fancybox/jquery.fancybox.pack.js?v=2.1.4"><\/script>')
    window.jQuery.fancybox.helpers.buttons || document.write('<script src="{{ site.baseurl }}/assets/javascripts/fancybox/helpers/jquery.fancybox-buttons.js?v=1.0.5"><\/script>')
</script>

<script>
    $("head").append('<link rel="stylesheet" href="{{ site.baseurl }}/assets/javascripts/fancybox/jquery.fancybox.css?v=2.1.4" type="text/css" />');
    $("head").append('<link rel="stylesheet" href="{{ site.baseurl }}/assets/javascripts/fancybox/helpers/jquery.fancybox-buttons.css?v=1.0.5" type="text/css" />');
    $(".post-image").fancybox({
        prevEffect: 'none',
        nextEffect: 'none',
        closeBtn: true,
        helpers: {
            title: {
                type: 'float'
            }
        }
    });
    $(document).ready(function () {
        $(".post-image > img").unveil(450);
    });
</script>
